package org.jeecg.common.api.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;


/**
 *
 * @author 杨峰
 *
 * @param <T>
 */
@Data
public class BaseEntity<T>  {

	@ApiModelProperty(hidden = true)
	protected  String createBy;

	@ApiModelProperty(hidden = true)
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	protected  Date   createTime;

	@ApiModelProperty(hidden = true)
	protected  String updateBy;

	@ApiModelProperty(hidden = true)
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	protected  Date   updateTime;

	/**
	 * 预新增，新增方法之前调用
	 */
	public void preInsert(){

		this.createTime = new Date();
		LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
		if(loginUser != null){
			this.createBy = loginUser.getUsername();
		}

	}

	/**
	 * 预修改，修改方法之前调用
	 */
	public void preUpdate(){
		this.updateTime = new Date();
		LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
		if(loginUser != null){
			this.updateBy = loginUser.getUsername();
		}
	}


}
